package com.yztc.orangeentertainment.ui.main.subscribefragment.starfragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.yztc.orangeentertainment.R;
import com.yztc.orangeentertainment.adapter.SubStarAdapter;
import com.yztc.orangeentertainment.base.BaseFragment;
import com.yztc.orangeentertainment.base.OnItemClickListener;
import com.yztc.orangeentertainment.bean.subscribebean.StarBean;
import com.yztc.orangeentertainment.config.SubscribeUrlConfig;
import com.yztc.orangeentertainment.ui.main.subscribefragment.staritem.StarItemActivity;
import com.yztc.orangeentertainment.utils.DividerItemDecoration;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by My on 2016/10/5.
 * 订阅明星fragment
 */
public class StarFragment extends BaseFragment implements StarContract.View{
    @InjectView(R.id.rv_star)
    RecyclerView mRecyclerView;
    private int page=1;//页数
    private int index;//viewpager对应的fragment个数
    private com.yztc.orangeentertainment.ui.main.subscribefragment.starfragment.StarPresenter presenter;



    @Override
    public int getLayoutId() {
        return R.layout.fragment_sub_star;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.inject(this, rootView);
        initView();
        initPresenter();
        return rootView;
    }

    /**
     * 初始化presenter
     */
    private void initPresenter() {

        Map<String, String> map = new HashMap<>();
        //参数map
        map.put(SubscribeUrlConfig.Params.PAGE, page + "");
        map.put(SubscribeUrlConfig.Params.TS, SubscribeUrlConfig.All_DefaultVaule.TS_VALUE);
        map.put(SubscribeUrlConfig.Params.ISLOGIN, SubscribeUrlConfig.All_DefaultVaule.ISLOGIN_VALUE);
        map.put(SubscribeUrlConfig.Params.MAC, SubscribeUrlConfig.All_DefaultVaule.MAC_VALUE);
        map.put(SubscribeUrlConfig.Params.UID, SubscribeUrlConfig.All_DefaultVaule.UID_VALUE);
        map.put(SubscribeUrlConfig.Params.PF, SubscribeUrlConfig.All_DefaultVaule.PF_VALUE);
        map.put(SubscribeUrlConfig.Params.NET, SubscribeUrlConfig.All_DefaultVaule.NET_VALUE);
        map.put(SubscribeUrlConfig.Params.ACCESSTOKEN, SubscribeUrlConfig.All_DefaultVaule.ACCESSTOKEN_VALUE);
        map.put(SubscribeUrlConfig.Params.CHANNEL, SubscribeUrlConfig.All_DefaultVaule.CHANNEL_VALUE);
        map.put(SubscribeUrlConfig.Params.VER, SubscribeUrlConfig.All_DefaultVaule.VER_VALUE);
        map.put(SubscribeUrlConfig.Params.RES, SubscribeUrlConfig.All_DefaultVaule.RES_VALUE);
        presenter.getStarFromNet(map,getActivity());
    }

    /**
     * 初始化数据
     */
    private void initView() {
        //获取上一层的数据
        Bundle bundle=getArguments();
        if(bundle!=null){
            index=bundle.getInt("index");
            switch (index){
                case 1:
                    presenter =new StarPresenter(this,1);
                    break;
                case 2:
                    presenter =new StarPresenter(this,2);
                    break;
                case 3:
                    presenter =new StarPresenter(this,3);
                    break;
            }
        }
        //mRecyclerView管理器
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity()){
            @Override
            public boolean canScrollHorizontally() {
                //使mRecyclerView不滑动
                return false;
            }
        };
        //水平摆放
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        //间距
        mRecyclerView.addItemDecoration(new DividerItemDecoration(20));
        mRecyclerView.setLayoutManager(linearLayoutManager);

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.reset(this);
    }


    /**
     * 将获取的list集合添加到adapter中
     * @param adapter
     * @param list
     */
    @Override
    public void sendToAdapter(SubStarAdapter adapter, final List<StarBean> list) {
        //加载adapter
        mRecyclerView.setAdapter(adapter);
        //mRecyclerView item点击事件
        adapter.setListener(new OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Intent intent=new Intent(getActivity(), StarItemActivity.class);
                intent.putExtra("sid",list.get(position).getId()+"");
                startActivity(intent);
            }

            @Override
            public void onItemLongClick(View view, int position) {

            }
        });
    }

    @Override
    public void onLoadStarSuccess(List<StarBean> starBeanList) {

    }

    @Override
    public void onLoadStarFail(String msg) {
        Toast.makeText(getActivity(),msg, Toast.LENGTH_SHORT).show();
    }
}
